import { showToast } from '@/utils/util';

export enum MapEnum {
  amap,
  baidu,
  tencent,
  google,
}

export class Map {
  name = '';
  packageName = '';
  iosSchema = '';
  androidUrl = '';
  iosUrl = '';

  constructor(name: string, packageName: string, iosSchema: string, androidUrl: string, iosUrl: string) {
    this.name = name;
    this.packageName = packageName;
    this.iosSchema = iosSchema;
    this.androidUrl = androidUrl;
    this.iosUrl = iosUrl;
  }
}

export const getMap = (map: MapEnum) => {
  switch (map) {
    case MapEnum.amap:
      return new Map(
        '高德地图',
        'com.autonavi.minimap',
        'iosamap://',
        'amapuri://route/plan/?sid=&did=&dlat=${latitude}&dlon=${longitude}&dname=${name}&dev=0&t=0',
        'iosamap://path?sourceApplication=fuxishan_uni_client&dlat=${latitude}&dlon=${longitude}&dname=${name}&dev=0&t=0',
      );
    case MapEnum.baidu:
      return new Map(
        '百度地图',
        'com.baidu.BaiduMap',
        'baidumap://',
        'baidumap://map/direction?origin&destination=name:${name}|latlng:${latitude},${longitude}&coord_type=wgs84&mode=driving&src=',
        'baidumap://map/direction?origin=&destination=name:${name}|latlng:${latitude},${longitude}&mode=driving&src=',
      );
    case MapEnum.tencent:
      return new Map(
        '腾讯地图',
        'com.tencent.map',
        'qqmap://',
        'qqmap://map/routeplan?type=drive&to=${name}&tocoord=${latitude},${longitude}&referer=',
        'qqmap://map/routeplan?type=drive&to=${name}&tocoord=${latitude},${longitude}&referer=',
      );
    case MapEnum.google:
      return new Map(
        '谷歌地图',
        'com.google.android.apps.maps',
        '',
        'google.navigation:q=${latitude},${longitude}',
        'comgooglemaps://?saddr=&daddr=${latitude},${longitude}',
      );
  }
};

export const openMap = (_map: MapEnum, latitude: string, longitude: string, addressName: string) => {
  const map = getMap(_map);
  if (!map) {
    //地图不存在报错
    return;
  }
  // #ifdef APP_PLUS
  const platform = uni.getSystemInfoSync().platform;
  if (platform == 'android') {
    if (!plus.runtime.isApplicationExist({ pname: map.packageName })) {
      //TODO 提示地图未安装
      showToast('地图未安装');
      return;
    }
    const url = map.androidUrl.replace('${latitude}', latitude).replace('${longitude}', longitude).replace('${name}', addressName);
    // url = encodeURIComponent(url);
    console.log('url->', url);
    plus.runtime.openURL(
      url,
      function (res) {
        //调起结果
        console.log('openMap error=>', res);
      },
      map.packageName,
    );
  }
  if (platform == 'ios') {
    if (!plus.runtime.isApplicationExist({ pname: map.iosSchema })) {
      //TODO 提示地图未安装
      showToast('地图未安装');
      return;
    }
    let url = map.iosUrl.replace('${latitude}', latitude).replace('${longitude}', longitude).replace('${name}', addressName);
    url = encodeURIComponent(url);
    plus.runtime.openURL(url, (res) => {
      //调起结果
      console.log('openMap error=>', res);
    });
  }
  // #endif

  // #ifdef MP
  // #endif
};
